package com.wtx.leetcode;

import java.util.Stack;

/**
 * @description: leetcode 1021号 删除最外层的括号
 * @author: wtx
 * @createDate: 2020/5/25
 */
public class Solution1021 {

    public String removeOuterParentheses(String S) {

        StringBuilder ret = new StringBuilder();
        Stack<Character> left = new Stack<>();
        Stack<Character> right = new Stack<>();
        for (char s: S.toCharArray()) {

            if (s == '(')
                left.push(s);
            else
                right.push(s);
            if (!right.isEmpty() && !left.isEmpty() ){

                Character leftPop = left.pop();
                Character rightPop = right.pop();
                if (!left.isEmpty())    //表示不是最外层括号
                    ret.append(leftPop).append(rightPop);
            }
        }
        return null;
    }
}
